package com.gking.processHarvest.service.Notes;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.processHarvest.common.BaseContext;
import com.gking.processHarvest.common.Res;
import com.gking.processHarvest.entity.Example;
import com.gking.processHarvest.entity.Notes;
import com.gking.processHarvest.mapper.NotesMapper;
import com.gking.processHarvest.service.Example.ExampleService;
import com.gking.processHarvest.service.Notes.NotesService;
import com.gking.processHarvest.utils.CheckStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)//三级缓存解决bean循环注入问题
public class NotesServiceImpl extends ServiceImpl<NotesMapper, Notes> implements NotesService {

    @Autowired
    private ExampleService exampleService;


    /**
     * 创建笔记
     *
     * @param projectId   所属项目id
     * @param exampleId   所属示例id
     * @param showContent 展示的内容
     * @param editContent 编辑的内容
     * @return s
     */
    @Override
    public Res<String> create(String projectId, String exampleId, String showContent, String editContent) {
        if (projectId == null || exampleId == null || showContent == null || editContent == null)
            return Res.error("请将参数补充完整");

        //是否存在且未删除
        CheckStatus<Example> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Example> queryWrapper = checkStatus.checkIsDelete(
                exampleId, Example::getId,
                Example::getMasterId, Example::getIsDelete);
        if (exampleService.getOne(queryWrapper) == null) return Res.error("示例已删除，无法添加笔记！");

        //是，则创建
        Notes notes = new Notes();
        notes.setProjectId(projectId);
        notes.setExampleId(exampleId);
        notes.setContent(showContent);
        notes.setEditContent(editContent);

        this.save(notes);

        return Res.success("创建笔记成功！");
    }

    /**
     * 更新笔记
     *
     * @param noteId      笔记id
     * @param showContent 展示的内容
     * @param editContent 编辑的内容
     * @return s
     */
    @Override
    public Res<String> update(String noteId, String showContent, String editContent) {
        if (noteId == null || showContent == null || editContent == null)
            return Res.error("请将参数补充完整");

        //笔记是否存在且未删除
        CheckStatus<Notes> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Notes> queryWrapper = checkStatus.checkIsDelete(
                noteId, Notes::getId,
                Notes::getMasterId, Notes::getIsDelete);
        if (this.getOne(queryWrapper) == null) return Res.error("笔记已删除，无法添加笔记！");

        //是，则更新
        LambdaUpdateWrapper<Notes> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Notes::getId, noteId);
        updateWrapper.eq(Notes::getMasterId, BaseContext.getCurrentId());
        updateWrapper.set(Notes::getContent, showContent);
        updateWrapper.set(Notes::getEditContent, editContent);

        this.update(updateWrapper);

        return Res.success("修改成功！");
    }


    /**
     * 删除笔记
     *
     * @param noteId 笔记id
     * @return s
     */
    @Override
    public Res<String> delete(String noteId) {
        if (noteId == null || noteId.equals("")) return Res.error("请将参数补充完整");

        //笔记是否存在且未删除
        CheckStatus<Notes> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Notes> queryWrapper = checkStatus.checkIsDelete(
                noteId, Notes::getId,
                Notes::getMasterId, Notes::getIsDelete);
        if (this.getOne(queryWrapper) == null) return Res.error("删除笔记失败！");

        //是，则删除
        LambdaUpdateWrapper<Notes> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Notes::getId, noteId);
        updateWrapper.eq(Notes::getMasterId, BaseContext.getCurrentId());
        updateWrapper.set(Notes::getIsDelete, 1);
        updateWrapper.set(Notes::getDeleteTime, LocalDateTime.now());

        this.update(updateWrapper);

        return Res.success("删除成功！");
    }


    /**
     * 获取笔记详情
     *
     * @param noteId 笔记id
     * @return s
     */
    @Override
    public Res<Notes> getDetail(String noteId) {
        CheckStatus<Notes> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Notes> wrapper = checkStatus.checkIsDelete(
                noteId, Notes::getId,
                Notes::getMasterId, Notes::getIsDelete);
        Notes one = this.getOne(wrapper);
        if (one == null) return Res.error("笔记不存在！");

        return Res.success("获取笔记详情成功！", one);
    }

}
